<?php
namespace Wap\Controller;
use Think\Controller;
class BaseoaController extends Controller {
	
	public static $wx;//缓存微信对象
	protected function _initialize(){
		if(!strpos($_SERVER["HTTP_USER_AGENT"],"MicroMessenger")){
			$this->diemsg(0,'请使用微信浏览器访问本应用！');
		}
		$options['appid']=C('BB_APPID');
		$options['secret']=C('BB_SECRET');
		self::$wx=new \bb\Wechat($options);
		if(I('code')){
			$d=self::$wx->getOauthAccessToken();
			$user=self::$wx->getOauthUserInfo($d['access_token'],$d['openid']);
			if($user['openid']){
				$_SESSION['wxuser']=$user;
				$_SESSION['set']['url']=C('BB_URL');
				//没有关注该公众号的微信用户信息
				p($user);
				
				
			}else{
				$oa_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
				$url=$this->snsapi_userinfo($oa_url);
				header("location:".$url);
			}
		}else{
			$oa_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$url=$this->snsapi_userinfo($oa_url);
			header("location:".$url);
		}
	}
	public function index(){

		
	}
	//用户手动同意授权URl
	public function snsapi_userinfo($url){
		return self::$wx->getOAuthRedirect($url,'STATE','snsapi_userinfo');
	}
	//停止不动的信息通知页面处理
	public function diemsg($status,$msg){
		//成功为1，失败为0
		$status=$status?$status:'0';
		$this->assign('status',$status);
		$this->assign('msg',$msg);
		$this->display('Base_diemsg');
		die();
	}
}